package com.ley.yikang.util;

import com.ley.yikang.exception.TokenException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class RestExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResultData<String> exception(Exception e) {
        System.out.println("全局异常信息 ex={" + e.getMessage() + "}");
        //判断异常类型
        if (e instanceof TokenException) {
            return ResultData.fail(401, e.getMessage());
        }
        return ResultData.fail(e.getMessage());
    }
}
